package com.spdzen.controller.admin;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spdzen.model.SysResource;
import com.spdzen.service.SysResourceService;
import com.spdzen.support.JsonUtils;
import com.spdzen.support.LogUtils;
import com.spdzen.support.exception.ResultCodeException;
import com.spdzen.support.result.JsonResult;
import com.spdzen.support.result.ResultCode;
import com.spdzen.support.validation.AddValidGroup;
import com.spdzen.support.validation.UpdateValidGroup;

@Controller
@RequestMapping("/admin/sysresource")
public class SysResourceAdminController {
	@Resource
	private SysResourceService sysResourceService;

	@RequestMapping("/pageView.do")
	public String pageView() {
		return "/admin/sysresource/sysResourcePageView";
	}

	@RequestMapping("/queryForTree.do")
	@ResponseBody
	public JsonResult queryAllForTree() {
		try {
			return JsonResult.success(sysResourceService.selectAllAdminVOForTree());
		} catch (Exception e) {
			LogUtils.getLogger().error("", e);
			return JsonResult.error();
		}
	}
	
	@RequestMapping("/queryById.do")
	@ResponseBody
	public JsonResult queryById(Long id) {
		if (id == null || id.longValue() < 1) {
			return JsonResult.errorParameter();
		}
		
		try {
			return JsonResult.success(sysResourceService.selectOneAdminVO(id));
		} catch (Exception e) {
			LogUtils.getLogger().error(Long.toString(id), e);
			return JsonResult.error();
		}
	}

	@RequestMapping("/delete.do")
	@ResponseBody
	public JsonResult delete(Long id) {
		if (id == null || id.longValue() < 1) {
			return JsonResult.errorParameter();
		}

		JsonResult jsonResult = null;

		try {
			sysResourceService.deleteIfPermission(id);
			jsonResult = JsonResult.success();
		} catch (ResultCodeException e) {
			ResultCode rc = e.getResultCode();
			if (rc != null) {
				jsonResult = JsonResult.error(rc);
			} else {
				jsonResult = JsonResult.error();
			}
		} catch (Exception e) {
			LogUtils.getLogger().error(Long.toString(id), e);
			jsonResult = JsonResult.error();
		}

		return jsonResult;
	}

	@RequestMapping(path = { "/add.do" }, method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult add(@Validated(value = { AddValidGroup.class }) SysResource record, BindingResult bindingResult, HttpServletRequest request) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}

		record.setOperateTime(new Date());
		record.setOperateId(0L);

		JsonResult jsonResult = null;

		try {
			sysResourceService.insertIfPermission(record);
			jsonResult = JsonResult.success(record);
		} catch (ResultCodeException e) {
			ResultCode rc = e.getResultCode();
			if (rc != null) {
				jsonResult = JsonResult.error(rc);
			} else {
				jsonResult = JsonResult.error();
			}
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(record), e);
			jsonResult = JsonResult.error();
		}

		return jsonResult;
	}

	@RequestMapping(path = { "/update.do" }, method = { RequestMethod.POST })
	@ResponseBody
	public JsonResult update(@Validated(value = { UpdateValidGroup.class }) SysResource record, BindingResult bindingResult, HttpServletRequest request) {
		if (bindingResult.hasErrors()) {
			return JsonResult.errorParameter();
		}
		
		if(StringUtils.isBlank(record.getIcon())){
			record.setIcon(" ");
		}
		
		if(StringUtils.isBlank(record.getUrl())){
			record.setUrl(" ");
		}
		
		if(record.getParentId() == null || record.getParentId().longValue() < 1){
			record.setParentId(0L);
		}

		record.setOperateTime(new Date());
		record.setOperateId(0L);

		JsonResult jsonResult = null;

		try {
			sysResourceService.updateIfPermission(record);
			jsonResult = JsonResult.success();
		} catch (ResultCodeException e) {
			ResultCode rc = e.getResultCode();
			if (rc != null) {
				jsonResult = JsonResult.error(rc);
			} else {
				jsonResult = JsonResult.error();
			}
		} catch (Exception e) {
			LogUtils.getLogger().error(JsonUtils.toJSONString(record), e);
			jsonResult = JsonResult.error();
		}

		return jsonResult;
	}
}
